package com.example.depcontrol.controller;

import com.example.depcontrol.pojo.Result;
import com.example.depcontrol.utils.AliOSSUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
//        log.info("文件上传：{}， {}，{}", username, age, image);
//
//        String s = image.getOriginalFilename();//获取文件名
//        String newFileName = UUID.randomUUID().toString()+s.substring(s.lastIndexOf("."));//通过UUID构建文件名
//        image.transferTo(new File("D:\\java-test\\image\\" + newFileName));//存储到本地
//
//        return Result.success();
//    }

    @Resource
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传:{}", image.getOriginalFilename());
        String url = aliOSSUtils.upload(image);
        log.info("文件访问的URL：{}", url);
        return Result.success(url);
    }
}
